#
# Copyright 2014-2019, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#
#     * Neither the name of the copyright holder nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
# src/test/Makefile -- build all unit tests
#
# Makefile -- build all unit tests
#

include ../common.inc

TEST_DEPS = \
	unittest\
	tools

OTHER_TESTS = \
	out_err\
	out_err_mt\
	scope\
	set_funcs\
	traces\
	traces_custom_function\
	unicode_api\
	unicode_match_script\
	util_file_create\
	util_file_open\
	util_is_absolute\
	util_is_zeroed\
	util_map_proc\
	util_parse_size

VMEM_TESTS = \
	vmem_aligned_alloc\
	vmem_calloc\
	vmem_check_allocations\
	vmem_check_version\
	vmem_check\
	vmem_create\
	vmem_create_error\
	vmem_create_in_region\
	vmem_custom_alloc\
	vmem_malloc\
	vmem_malloc_usable_size\
	vmem_mix_allocations\
	vmem_multiple_pools\
	vmem_out_of_memory\
	vmem_pages_purging\
	vmem_realloc\
	vmem_realloc_inplace\
	vmem_stats\
	vmem_strdup\
	vmem_valgrind\
	vmem_valgrind_region

VMMALLOC_DUMMY_FUNCS_DEPS = \
	vmmalloc_dummy_funcs

VMMALLOC_DUMMY_FUNCS_TESTS = \
	vmmalloc_malloc_hooks\
	vmmalloc_memalign\
	vmmalloc_valloc

VMMALLOC_TESTS = \
	vmmalloc_calloc\
	vmmalloc_check_allocations\
	vmmalloc_fork\
	vmmalloc_init\
	vmmalloc_malloc\
	vmmalloc_malloc_usable_size\
	vmmalloc_out_of_memory\
	vmmalloc_realloc\
	vmmalloc_valgrind

LOCAL_TESTS = \
	$(OTHER_TESTS)\
	$(VMEM_TESTS)\
	$(VMMALLOC_DUMMY_FUNCS_TESTS)\
	$(VMMALLOC_TESTS)

TESTS = $(LOCAL_TESTS)

TESTS_BUILD = \
	$(TEST_DEPS)\
	$(VMMALLOC_DUMMY_FUNCS_DEPS)\
	$(TESTS)

all     : TARGET = all
clean   : TARGET = clean
clobber : TARGET = clobber
test    : TARGET = test
cstyle  : TARGET = cstyle
format  : TARGET = format
check   : TARGET = check
pcheck  : TARGET = pcheck
sparse  : TARGET = sparse

DIR_SYNC=$(TOP)/src/test/.sync-dir
SYNC_EXT=synced
TESTCONFIG=$(TOP)/src/test/testconfig.sh
FILE_MAX_DAX_DEVICES=$(TOP)/src/test/tools/anonymous_mmap/max_dax_devices

all test format sparse: $(TESTS_BUILD)

cstyle: $(TESTS_BUILD)
	$(CHECK_SHEBANG) $(foreach dir,$(TESTS_BUILD),$(dir)/TEST? $(dir)/TEST??)

clean clobber: $(TESTS_BUILD)
	$(RM) -r $(DIR_SYNC)
	$(RM) *.$(SYNC_EXT)
	$(RM) $(FILE_MAX_DAX_DEVICES)

$(TESTS) $(VMMALLOC_DUMMY_FUNCS_DEPS): $(TEST_DEPS)

$(VMMALLOC_DUMMY_FUNCS_TESTS): $(VMMALLOC_DUMMY_FUNCS_DEPS)

$(TESTS_BUILD):
	$(MAKE) -C $@ $(TARGET)

memcheck-summary:
	grep ERROR */memcheck*.log

memcheck-summary-errors:
	grep ERROR */memcheck*.log | grep -v " 0 errors" || true

memcheck-summary-leaks:
	grep "in use at exit" */memcheck*.log | grep -v " 0 bytes in 0 blocks" || true

check:
	@./RUNTESTS $(RUNTEST_OPTIONS) $(TESTS)
	@echo "No failures."

pcheck: pcheck-local-quiet
	@echo "No failures."

pcheck-other: TARGET = pcheck
pcheck-other: $(OTHER_TESTS)
	@echo "No failures."

pcheck-vmem: TARGET = pcheck
pcheck-vmem: $(VMEM_TESTS)
	@echo "No failures."

pcheck-vmmalloc: TARGET = pcheck
pcheck-vmmalloc: $(VMMALLOC_TESTS)
	@echo "No failures."

pcheck-local-quiet: TARGET = pcheck
pcheck-local-quiet: $(LOCAL_TESTS)

pcheck-local: pcheck-local-quiet
	@echo "No failures."

TESTCONFIG=$(TOP)/src/test/testconfig.sh

$(TESTCONFIG):

SUPP_SYNC_FILES=$(shell echo *.supp | sed s/supp/$(SYNC_EXT)/g)

%.$(SYNC_EXT): %.supp $(TESTCONFIG)
	cp $(shell echo $^ | cut -d" " -f1) $(DIR_SYNC)
	@touch $@

.PHONY: all check clean clobber cstyle pcheck\
	 pcheck-other pcheck-vmem pcheck-vmmalloc\
	 test unittest tools format \
	 pcheck-local $(TESTS_BUILD)
